using System;
using System.IO;
using System.Collections;
using ClickNET.Imaging;

namespace ClickNET
{
    // Takes an array of file names or directory names on the command line.  
    // Determines what kind of name it is and processes it appropriately
    public class DirectoryProcessor
    {
        public delegate void ProcessFileDelegate(string fileLocation);
        public event ProcessFileDelegate ProcessFile;

        private string m_fullPath;

        public DirectoryProcessor(string fullPath)
        {
            this.m_fullPath = fullPath;
        }

        /// <summary>
        /// Recursively processes a given path
        /// </summary>
        public void Process()
        {
            if (null == ProcessFile)
                throw new Exception("ProcessFile needs to be wired up.");

            if (File.Exists(this.m_fullPath))
                ProcessFile(this.m_fullPath); // This path is a file
            else if (Directory.Exists(this.m_fullPath))
                ProcessDirectory(this.m_fullPath); // This path is a directory
            else
            {
                Log.DefaultLog.WriteLine(String.Format("{0} is not a valid file or directory.", this.m_fullPath));
            }
        }

        /// <summary>
        /// Process all files in the directory passed in, and recurse on any directories 
        /// </summary>
        /// <param name="targetDirectory">Directory to parse</param>
        private void ProcessDirectory(string targetDirectory)
        {
            // Process the list of files found in the directory
            string[] fileEntries = Directory.GetFiles(targetDirectory);
            foreach (string fileName in fileEntries)
                ProcessFile(fileName);

            // Recurse into subdirectories of this directory
            string[] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
            foreach (string subdirectory in subdirectoryEntries)
                ProcessDirectory(subdirectory);
        }
    }
}
